package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import net.realsee.common.utils.StringUtils;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import net.realsee.domain.wcs.data.bo.WcsDefectTypeBo;
import net.realsee.domain.wcs.data.vo.WcsDefectTypeVo;
import net.realsee.domain.wcs.data.WcsDefectType;
import net.realsee.mapper.wcs.data.WcsDefectTypeMapper;
import net.realsee.service.wcs.data.IWcsDefectTypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 不良类型定义Service业务层处理
 *
 * @author realsee
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class WcsDefectTypeServiceImpl extends ServiceImpl<WcsDefectTypeMapper, WcsDefectType> implements IWcsDefectTypeService {

    private final WcsDefectTypeMapper baseMapper;

    /**
     * 查询不良类型定义
     */
    @Override
    public WcsDefectTypeVo selectVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询不良类型定义列表
     */
    @Override
    public TableDataInfo<WcsDefectTypeVo> selectVoPage(WcsDefectTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsDefectType> lqw = buildVoQueryWrapper(bo);
        Page<WcsDefectTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询不良类型定义列表
     */
    @Override
    public List<WcsDefectTypeVo> selectVoList(WcsDefectTypeBo bo) {
        LambdaQueryWrapper<WcsDefectType> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsDefectType> buildVoQueryWrapper(WcsDefectTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsDefectType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDefectType()), WcsDefectType::getDefectType, bo.getDefectType());
        lqw.eq(StringUtils.isNotBlank(bo.getTypeDesc()), WcsDefectType::getTypeDesc, bo.getTypeDesc());
        return lqw;
    }

    /**
     * 新增不良类型定义
     */
    @Override
    public Boolean insertByBo(WcsDefectTypeBo bo) {
        WcsDefectType add = BeanUtil.toBean(bo, WcsDefectType.class);
        return this.save(add);
    }

    /**
     * 修改不良类型定义
     */
    @Override
    public Boolean updateByBo(WcsDefectTypeBo bo) {
        WcsDefectType update = BeanUtil.toBean(bo, WcsDefectType.class);
        return this.updateById(update);
    }

    /**
     * 批量删除不良类型定义
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
